import 'package:custom_widgets/item/item_normal_widget.dart';
import 'package:custom_widgets/page/parenting_animation_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ThemeDemoOtherPage extends StatefulWidget {
  @override
  _ThemeDemoOtherPageState createState() => _ThemeDemoOtherPageState();
}

// typedef Offset OffsetHandle(Animation animation);

// Offset fromLeft(Animation animation) {
//   return Offset(animation.value - 1, 0);
// }

// Offset fromRight(Animation animation) {
//   return Offset(1 - animation.value, 0);
// }

// Offset fromTop(Animation animation) {
//   return Offset(0, animation.value - 1);
// }

// Offset fromBottom(Animation animation) {
//   return Offset(0, 1 - animation.value);
// }

// Offset fromTopLeft(Animation anim) {
//   return fromLeft(anim) + fromTop(anim);
// }

class _ThemeDemoOtherPageState extends State<ThemeDemoOtherPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("主题 (Ohter)"),
        centerTitle: true,
        elevation: 0.0,
      ),
      body: _buildBody(),
    );
  }

  _buildBody() {
    // ...List.generate(model.themes.length, (index) {
    // 循环出元素
    // });
    return ItemTileTheme(
      textColor: Colors.blue,
      child: ListView(
        children: ItemNormalWidget.divideTiles(
          childs: <Widget>[
            ListTile(
              leading: GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  print("点击图标");
                },
                child: Container(
                  width: 48,
                  height: 48,
                  padding: EdgeInsets.symmetric(vertical: 4.0),
                  alignment: Alignment.center,
                  child: CircleAvatar(),
                ),
              ),
              title: Text('title'),
              trailing: Icon(Icons.ac_unit),
              onTap: () {
                print("点击整行");
              },
            ),
            // (AbsorbPointer) absorbing属性为true时禁用其子部件的点击事件
            AbsorbPointer(
              absorbing: false,
              child: ItemTileTheme(
                textColor: Colors.red,
                child: ItemNormalWidget(
                  title: Text("测试文本"),
                  onTap: () {},
                ),
              ),
            ),
            ItemNormalWidget(
              leading: Icon(Icons.account_circle),
              title: Text("测试文本2"),
              onTap: () {},
            ),
            ItemNormalWidget(
              title: Text("测试文本3"),
              onTap: () {
                Navigator.push<dynamic>(
                  context,
                  MaterialPageRoute<dynamic>(
                    builder: (BuildContext context) => ParentingAnimationPage(),
                  ),
                );
              },
            ),
            ItemNormalWidget(
              title: Text("测试文本4"),
              onTap: () {
                showDialogWithOffset();
              },
            ),
            ItemNormalWidget(
              title: Text("测试文本5"),
              onTap: () {
                showGeneralDialog(
                  context: context,
                  barrierLabel: "你好",
                  barrierDismissible: true,
                  transitionDuration: Duration(milliseconds: 1000),
                  barrierColor: Colors.black.withAlpha(128),
                  pageBuilder: (BuildContext context, Animation animation,
                      Animation secondaryAnimation) {
                    return Center(
                      child: Material(
                        child: Container(
                          color: Colors.black.withAlpha(animation.value),
                          child: Text("我是一个可变的"),
                        ),
                      ),
                    );
                  },
                );
              },
            ),
            ItemNormalWidget(
              title: Text("日期选择"),
              onTap: () {
                showCupertinoModalPopup(
                  context: context,
                  builder: (BuildContext contex) {
                    return Container(
                      height: 350.0,
                      child: CupertinoDatePicker(
                        backgroundColor: Theme.of(contex).canvasColor,
                        mode: CupertinoDatePickerMode.date,
                        initialDateTime: DateTime.now(),
                        onDateTimeChanged: (date) {
                          if (mounted) {
                            print("${date.year}-${date.month}-${date.day}");
                          }
                        },
                      ),
                    );
                  },
                ).then((val) {
                  print("$val");
                });
              },
            ),
          ],
        ).toList(),
      ),
    );
  }

  /// 侧边弹出菜单demo实现
  showDialogWithOffset() {
    showGeneralDialog(
      context: context,
      barrierColor: Colors.black.withAlpha(128),
      barrierLabel: "",
      barrierDismissible: true,
      transitionDuration: const Duration(milliseconds: 300),
      pageBuilder: (
        BuildContext context,
        Animation animation,
        Animation secondaryAnimation,
      ) {
        return MediaQuery.removeViewInsets(
          removeLeft: true,
          removeTop: true,
          removeRight: true,
          removeBottom: true,
          context: context,
          child: Center(
            child: Container(
              // 这里的宽度,与底部的比例需要对应,不然内容会被截取调
              width: MediaQuery.of(context).size.width * 0.7,
              height: MediaQuery.of(context).size.height,
              color: Colors.red,
              child: Material(
                child: Container(
                  child: Column(
                    children: <Widget>[
                      SafeArea(
                        child: Text("我是标题我是标题我是标题我是标题我是标题我是标题我是标题我是标题"),
                      ),
                      TextField(),
                      Spacer(),
                      SafeArea(
                        child: MaterialButton(
                          child: Text("点击关闭"),
                          onPressed: () {
                            Navigator.pop(context, "我是返回值");
                          },
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        );
      },
      transitionBuilder: (ctx, animation, _, child) {
        return FractionalTranslation(
          // translation: handle(animation),
          // 这里1.2 控制打开的宽度(需要自己去定义)
          translation: Offset(1.15 - animation.value, 0),
          child: child,
        );
      },
    ).then((val) {
      // 这里可以获取到内部返回的值
      print(val);
    });
  }
}
